A WeakMap is a collection in JavaScript that allows you to store key-value pairs where the keys are weakly held, meaning they don't prevent the garbage collection of their associated values. This is useful for scenarios where you want to associate data with objects without preventing their disposal.
In a regular Map, as long as the Map exists, the objects used as keys will stay in memory—even if you delete every other reference to that object. This can lead to memory leaks.
In a WeakMap, if there are no other references to the key object anywhere else in your code, the JavaScript engine's Garbage Collector is allowed to remove that object from memory and delete the corresponding entry in the WeakMap automatically.
Keys must be Objects: Unlike a regular Map (which can use strings or numbers as keys), a WeakMap only accepts objects (and registered symbols) as keys.
Not Iterable: You cannot loop through a WeakMap. There is no .forEach(), .keys(), or .size property.
How would you use a WeakMap to associate metadata with DOM elements without causing memory leaks?
What happens to entries in a WeakMap when there are no other references to the key object?
If you try to iterate over a WeakMap, what result do you get and why?
We have a caching layer that stores results keyed by request objects using a WeakMap, but sometimes the cache returns undefined. Walk me through how you'd debug this.
Explain why replacing a Map with a WeakMap in a library that stores event listeners caused a regression in some browsers.
When would you choose a WeakMap over a regular Map for implementing memoization, and what trade‑offs are involved?
Design a component that tracks user session objects and needs to automatically clean up when sessions are garbage‑collected. How would you structure it with WeakMap, and what performance considerations arise at scale?
Suppose you need to implement private fields for a class hierarchy across multiple modules without exposing them. How would you use WeakMap, and what are the pitfalls regarding serialization and debugging?
If a large application suffers from memory bloat due to lingering references, how could you refactor parts of it to use WeakMap to mitigate the issue, and what monitoring would you put in place?
Your organization is planning to migrate a legacy codebase that uses hidden properties on objects for internal state to a WeakMap‑based approach for better encapsulation. What architectural changes, testing strategies, and cross‑team coordination would you recommend?
In a shared library that will be consumed by many teams, you want to expose an API that stores per‑instance data without preventing garbage collection. How would you design the API using WeakMap, and how would you document its constraints to avoid misuse?
Consider a server‑side rendering framework that caches component instances per request using WeakMap. Discuss the long‑term maintenance implications and how you’d ensure it doesn’t become a source of subtle memory leaks.